﻿using MediatR;
using WMS.API.Application.Command.StocktakingManagement.PendingOrder;
using WMS.Domain.Entities.StocktakingManagement;
using WMS.ErrorCode;
using WMS.Interfactructure.Interfaces;

namespace WMS.API.Application.CommandHandlers.StocktakingManagement
{
    /// <summary>
    /// 批量删除待盘点订单命令处理-xzc
    /// </summary>
    public class DeletePendingAnageOrdersCommandHandlers : IRequestHandler<DeletePendingAnageOrdersCommand, APIResult<int>>
    {
        private readonly IBaseRepository<PendingOrders> _pendingordersRepository;

        public DeletePendingAnageOrdersCommandHandlers(IBaseRepository<PendingOrders> pendingordersRepository)
        {
            _pendingordersRepository = pendingordersRepository;
        }

        public async Task<APIResult<int>> Handle(DeletePendingAnageOrdersCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>("批量删除成功");

            result.Message = "批量删除成功";
            result.Code = ResultCode.Success;

            string[] pid = request.Id.Split(',');
            int a = 0;
            foreach (var item in pid)
            {
                a = await _pendingordersRepository.DeletteRangeAsync(x => x.Id == Convert.ToInt32(item));
            }

            result.Data = a;

            return await Task.FromResult(result);
        }
    }
}
